package com.qianke.common;

import com.qianke.common.exception.BizException;
import com.qianke.common.exception.ParamException;
import com.qianke.domain.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

public abstract class Assert {

    public static void assertNotMinus(Integer i, String msg) {
        if (i.compareTo(0) == -1) throw new BizException(Constants.CODE_PARAM_ERROR, msg);
    }

    public static void assertNotZero(Integer i, String msg) {
        if (i.compareTo(0) == 0) throw new BizException(Constants.CODE_PARAM_ERROR, msg);
    }

    public static void assertNotMinusAndZero(Integer i, String msg) {
        if (i.compareTo(0) == -1 || i.compareTo(0) == 0) throw new BizException(Constants.CODE_PARAM_ERROR, msg);
    }

    public static void assertNotNull(Object param) {
        Assert.assertNotNull(Arrays.asList(param), Constants.CODE_PARAM_ERROR, "参数缺失!");
    }

    public static void assertNotNull(Object param, String msg) {
        Assert.assertNotNull(Arrays.asList(param), Constants.CODE_PARAM_ERROR, msg);
    }

    public static void assertNotEquals(String paramOne, String paramTwo, String msg) {
        if (!StringUtils.equals(paramOne, paramTwo)) throw new BizException(Constants.CODE_PARAM_ERROR, msg);
    }

    public static void assertNotNull(Object param, String code, String msg) {
        Assert.assertNotNull(Arrays.asList(param), code, msg);
    }

    public static void assertNotNull(List<Object> paramList) {
        assertNotNull(paramList, Constants.CODE_PARAM_ERROR, "参数缺失!");
    }

    public static void assertNotNull(List<Object> paramList, String msg) {
        assertNotNull(paramList, Constants.CODE_PARAM_ERROR, msg);
    }

    public static void assertNotNull(List<Object> paramList, String code, String msg) {
        Optional.ofNullable(paramList).ifPresent(list -> {
            list.forEach(item -> {
                if (item instanceof String) {
                    String str = (String) item;
                    if (StringUtils.isEmpty(str)) throw new ParamException(code, msg);
                } else if (item instanceof Collection) {
                    Collection<?> collections = (Collection<?>) item;
                    if (CollectionUtils.isEmpty(collections)) throw new ParamException(code, msg);
                } else {
                    Optional.ofNullable(item).orElseThrow(() -> {
                        return new ParamException(code, msg);
                    });
                }
            });
        });
    }
}
